const types = {
  SET_LOADING: 'SET_LOADING',
  SET_LANGUAGE: 'SET_LANGUAGE',
  TOGGLE_ASIDE_COLLAPSE: 'TOGGLE_ASIDE_COLLAPSE',
  LOGIN_SUCCESS: 'LOGIN_SUCCESS',
  LOGOUT: 'LOGOUT'
}

const state = {
  isLoading: false,
  language: 'zh',
  asideCollapse: true,
  isLogin: false,
  userInfo: {}
}

const mutations = {
  [types.SET_LOADING](state, flag) {
    state.isLoading = flag
  },
  [types.SET_LANGUAGE](state, lang) {
    state.language = lang
  },
  [types.TOGGLE_ASIDE_COLLAPSE](state) {
    state.asideCollapse = !state.asideCollapse
  },
  [types.LOGIN_SUCCESS](state, userInfo) {
    state.isLogin = true
    state.userInfo = userInfo
  },
  [types.LOGOUT](state) {
    state.isLogin = false
    state.userInfo = {}
  }
}

const actions = {
  setLoading({
    commit
  }, flag) {
    commit(types.SET_LOADING, flag)
  },
  setLanguage({
    commit
  }, lang) {
    commit(types.SET_LANGUAGE, lang)
  },
  toggleAsideCollapse({
    commit
  }) {
    commit(types.TOGGLE_ASIDE_COLLAPSE)
  },
  async login({
    commit
  }, loginForm) {
    await commit(types.LOGIN_SUCCESS, loginForm)
    return true
  },
  logout({
    commit
  }) {
    commit(types.LOGOUT)
  }
}

export default {
  state,
  mutations,
  actions
}
